package web

import (
	"gitee.com/tools/internal/domain"
	"gitee.com/tools/internal/service"
	"gitee.com/tools/internal/web/tools/common"
	"github.com/gin-gonic/gin"
	"net/http"
)

type SelectHandler struct {
	svc service.SelectService
}

func NewSelectHandler(svc service.SelectService) *SelectHandler {
	return &SelectHandler{svc: svc}
}

func (s *SelectHandler) Router(r *gin.Engine) {
	g := r.Group("/select")
	g.POST("/ip", s.selectIp)
	g.POST("/phone", s.selectPhone)
	g.POST("/country", s.selectCountryCode)
	g.POST("/dns", s.selectDns)
	g.POST("/domainWeight", s.DomainWeight)
}

func (s *SelectHandler) selectIp(ctx *gin.Context) {
	type Req struct {
		Ip     string `json:"ip"`
		IPType string `json:"ipType"`
	}
	var req Req
	if err := ctx.BindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrorUserInputInvalid,
			Message: "参数无效",
		})
		return
	}
	ipInfo, err := s.svc.SelectIp(ctx, req.Ip)
	if err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrSelectIP,
			Message: "查询失败",
		})
		return
	}
	ipInfo.Ip = req.Ip
	ctx.JSON(http.StatusOK, common.Result{
		Code:    0,
		Message: "查询成功",
		Data: map[string]any{
			"Ip":     req.Ip,
			"IpType": req.IPType,
			"IpINfo": ipInfo,
		},
	})
	return
}

func (s *SelectHandler) selectPhone(ctx *gin.Context) {
	type Req struct {
		Mobile string `json:"mobile"` // 需要查询的手机号码
		SaoRao string `json:"saorao"` //TRUE/FALSE (开启/关闭)是否查询骚扰号码状态(开启后查询速度会较慢)
	}
	var req Req
	if err := ctx.BindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrorUserInputInvalid,
			Message: "参数无效",
		})
		return
	}
	phoneInfo, err := s.svc.SelectPhone(ctx, req.Mobile, req.SaoRao)
	if err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrSelectPhone,
			Message: "查询失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, common.Result{
		Code:    0,
		Message: "查询成功",
		Data: map[string]any{
			"Phone":     req.Mobile,
			"PhoneINfo": phoneInfo,
		},
	})
	return
}

func (s *SelectHandler) selectCountryCode(ctx *gin.Context) {
	type Req struct {
		KeyWords string `json:"keyWords"`
		Type     string `json:"type"`
	}

	var req Req
	if err := ctx.BindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrorUserInputInvalid,
			Message: "参数无效",
		})
		return
	}
	CountryCode, err := s.svc.SelectCountryCode(ctx, req.KeyWords, req.Type)
	if err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrSelectCountryCode,
			Message: "查询失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, common.Result{
		Code:    0,
		Message: "查询成功",
		Data: map[string]any{
			"Country":     req.KeyWords,
			"Type":        req.Type,
			"CountryCode": CountryCode,
		},
	})
	return
}

func (s *SelectHandler) selectDns(ctx *gin.Context) {
	var req domain.Dns
	if err := ctx.BindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrorUserInputInvalid,
			Message: "参数无效",
		})
		return
	}
	result, err := s.svc.SelectDns(ctx, req)
	if err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrSelectDns,
			Message: "查询失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, common.Result{
		Code:    0,
		Message: "查询成功",
		Data: map[string]any{
			"domain": req.Domain,
			"Type":   req.Type,
			"result": result,
		},
	})
	return
}

func (s *SelectHandler) DomainWeight(ctx *gin.Context) {
	type Req struct {
		Domain string `json:"domain"`
	}
	var req Req
	if err := ctx.BindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrorUserInputInvalid,
			Message: "参数无效",
		})
		return
	}
	result, err := s.svc.SelectDomainWeight(ctx, req.Domain)
	if err != nil {
		ctx.JSON(http.StatusOK, common.Result{
			Code:    common.ErrSelectDomainWeight,
			Message: "查询失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, common.Result{
		Code:    0,
		Message: "查询成功",
		Data: map[string]any{
			"domain": req.Domain,
			"Type":   "json",
			"result": result,
		},
	})
	return
}
